package com.tanhua.server.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.server.service.UserService;
import com.tanhua.server.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Map;

@RestController
@RequestMapping("manage")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据条件(必须:page,pagesize 非必须:id,nickname,city)查询用户信息
     *
     * @param page     当前页
     * @param pagesize 每页页数
     * @param userId   用户id
     * @param nickName 昵称
     * @param city     城市
     * @return
     */
    @GetMapping("users")
    @NoAuthorization
    public ResponseEntity usersList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize,
                                    @RequestParam(value = "id", required = false) Integer userId,
                                    @RequestParam(value = "nickname", required = false) String nickName,
                                    @RequestParam(value = "city", required = false) String city
    ) {
        PageResult result = userService.usersList(page, pagesize, userId, nickName, city);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询单个用户的详细信息
     *
     * @param userId  用户id
     * @return
     */
    @GetMapping("users/{userID}")
    public ResponseEntity findUserInfoById(@PathVariable("userID") Integer userId){
        UserInfoVo userInfo = userService.findUserInfoById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 用户冻结操作
     *
     * @param params
     * @return
     */
    @PostMapping("users/freeze")
    @NoAuthorization
    public ResponseEntity freeze(@RequestBody Map<String,Object> params){
        String message = userService.freeze(params);
        System.out.println(params);
        return ResponseEntity.ok(message);
    }

    /**
     * 用户解冻操作
     *
     * @param params
     * @return
     */
    @PostMapping("users/unfreeze")
    @NoAuthorization
    public ResponseEntity unfreeze(@RequestBody Map<String,Object> params){
        String message = userService.unfreeze(params);
        System.out.println(params);
        return ResponseEntity.ok(message);
    }
}
